package com.example.apidocserver.infrastructure.util.pdf;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.BaseFont;

import java.io.IOException;

/**
 * pdf字体工具
 *
 * @author xyqlg
 */
public class PdfFontUtil {

    public static Font getFont(String fontPath, int fontSize, String fontName) {
        FontFactory.register(fontPath, fontName);
        // 字体大小
        return FontFactory.getFont(fontName, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, fontSize);
    }

    public static Font getFont() {
        // 字体文件路径
        String fontPath = "/fonts/xueshihei.ttf";
        // 字体名称，可自定义
        String fontName = "xueshihei";
        // 字体大小
        int fontSize = 12;
        return getFont(fontPath, fontSize, fontName);
    }

    public static Font getFont(String fileName) {
        // 字体文件路径
        String fontPath = "/fonts/" + fileName + ".ttf";
        // 字体大小
        int fontSize = 12;
        return getFont(fontPath, fontSize, fileName);
    }

    public static PdfFont getPdfFont(String fontPath, int fontSize, String fontName) throws IOException {
        PdfFont font = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
        return font;
    }

    public static PdfFont getPdfFont() throws IOException {
        // 字体文件路径
        String fontPath = "/fonts/xueshihei.ttf";
        // 字体名称，可自定义
        String fontName = "xueshihei";
        // 字体大小
        int fontSize = 12;
        return getPdfFont(fontPath, fontSize, fontName);
    }
}
